<?php
/**
 * Database field definition
 * bases
 */
class DbField {

    /**
     * import definition parameters
     * @param string $dbName database field name
     * @param bool $null is null value allowed ?
     * @param bool $auto is set automatically when saving ?
     */
    public function __construct($dbName,$null=true) {
        $this->dbName=$dbName;
        $this->null=$null;
    }

    /**
     * converts data for use in SQL queries
     * @param mixed $value
     * @return string
     */
    public function toDb($value) {
        return "'".addslashes($value)."'";
    }

    /**
     * converts data from database to PHP types
     * @param string $value
     * @return mixed
     */
    public function toPhp($value) {
        return $value;
    }

    /**
     * @return string name in database
     */
    public function getDbName() {
        return $this->dbName;
    }

    /**
     * tells if valid for db saving
     * @param mixed $value
     * @return bool
     */
    public function isValid($value) {
        if (!$value and !$this->null)
            return false;
        return true;
    }

    /**
     * you can define an automatic value there
     */
    // public function

    /**
     * defines as key
     * @param string $type primary
     * @return bool
     * /
    public function setKey($type) {
        $this->key=$type;
        return true;
    }/**/

    /**
     * is primary key ?
     * @return bool
     * /
    public function isPrimaryKey() {
        return $this->key=='primary';
    }/**/

    /**
     * @var string primary|null is key ?
     */
    //protected $key;

    /**
     *
     */
}

class DbFieldInvalid extends Exception {

    /*public function __construct($key,$field) {
        $this->field=$field;
        //$this->message=
    }*/
}